package entity

import (
	"errors"
	"net/http"

	pgdb "gsc-datasets/internal/db/sqlc"
	"gsc-datasets/internal/db/dao/ctype"
)

type Category struct {
	pgdb.Category
	Pid ctype.JsonNullInt64 `json:"pid"`
}

func ConvCategory(o pgdb.Category) *Category {
	ptr := &Category{
		Category: o,
		Pid:      ctype.JsonNullInt64{NullInt64: o.Pid},
	}
	return ptr
}

type ListCategoriesAsTreeRow struct {
	pgdb.ListCategoriesAsTreeRow
	Pid ctype.JsonNullInt64 `json:"pid"`
}

func ConvCategoryAsTreeRow(o pgdb.ListCategoriesAsTreeRow) *ListCategoriesAsTreeRow {
	ptr := &ListCategoriesAsTreeRow{
		ListCategoriesAsTreeRow: o,
		Pid:                     ctype.JsonNullInt64{NullInt64: o.Pid},
	}
	return ptr
}

type CreateCategoryRequest struct {
	*pgdb.CreateCategoryParams
	Pid ctype.JsonNullInt64 `json:"pid"`
}

func (s *CreateCategoryRequest) Bind(r *http.Request) error {
	if s.CreateCategoryParams == nil {
		return errors.New("缺少createCategory字段")
	}
	return nil
}

type CreateCategoryResponse = struct {
	InstancePtr *Category `json:"created"`
	Categories  []*ListCategoriesAsTreeRow `json:"categories"`
};
type UpdateCategoryResponse = struct {
	InstancePtr *Category `json:"updated"`
	Categories  []*ListCategoriesAsTreeRow `json:"categories"`
};

type DeleteCategoryResponse = struct {
	InstancePtr *Category `json:"deleted"`
	Categories  []*ListCategoriesAsTreeRow `json:"categories"`
};

type UpdateCategoryRequest struct {
	*pgdb.UpdateCategoryParams
	Pid ctype.JsonNullInt64 `json:"pid"`
}

func (s *UpdateCategoryRequest) Bind(r *http.Request) error {
	if s.UpdateCategoryParams == nil {
		return errors.New("缺少updateCategory字段")
	}
	return nil
}